import org.junit.Test;

import java.io.*;
import java.util.UUID;

/**
 * @author rockjh 【rockjh@aliyun.com】
 * @Description:
 * @Date 2018/1/30 21:27
 **/
public class InsertData {
    @Test
    public void menu() throws IOException {
        String path=InsertData.class.getClassLoader().getResource("").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path+ "menu.sql")));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+ "menu-new.sql")));

        String data = null;
        while ((data = br.readLine()) != null) {
            String temp="";
            temp=data.substring(0,data.indexOf("("))+"(id,"+
                    data.substring(data.indexOf("(")+1,data.lastIndexOf("("))+
                    "('"+UUID.randomUUID().toString().replaceAll("-","")+"',"+
                    data.substring(data.lastIndexOf("(")+1,data.length());
            bw.write(temp);
            bw.newLine();
        }
        bw.flush();
    }

    @Test
    public void dict() throws IOException {
        String path=InsertData.class.getClassLoader().getResource("").getPath();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path+ "dict.sql")));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path+ "dict-new.sql")));
        String pid="";
        String data = null;
        while ((data = br.readLine()) != null) {
            String uuid=UUID.randomUUID().toString().replaceAll("-","");
            String endData=data.substring(data.lastIndexOf("(")+1,data.length());
            String temp=data.substring(0,data.indexOf("("))+"(id,"+
                    data.substring(data.indexOf("(")+1,data.lastIndexOf("("))+
                    "('"+uuid+"',";
            String[] array=endData.split(",");
            if(array[1].equals(" 0")){
                pid=uuid;
                array[1]="'0'";
            }else {
                array[1]="'"+pid+"'";
            }
            for (int i=0;i<array.length;i++){
                temp += array[i];
                if(i!=array.length-1) {
                    temp+= ",";
                }
            }
            bw.write(temp);
            bw.newLine();
        }
        bw.flush();
    }
}
